﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace PhoneApplicPersons
{
    public partial class PAddPerson : PhoneApplicationPage
    {
        public PAddPerson()
        {
            InitializeComponent();
        }


        private void Btn_Cancel_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.NavigationService.GoBack();
            e.Complete();
            e.Handled = true;
        }

        private void Btn_Save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if ((Application.Current as App).persona == null)
                {
                    (Application.Current as App).list.Create(new Classes.Persona(Convert.ToInt32(TB_ID.Text), TB_FName.Text, TB_LName.Text, Convert.ToInt32(TB_Age.Text)));
                    MessageBox.Show("Новую запись созданно", "OK", MessageBoxButton.OK);
                }
                else
                {
                    (Application.Current as App).list.Update(new Classes.Persona(Convert.ToInt32(TB_ID.Text), TB_FName.Text, TB_LName.Text, Convert.ToInt32(TB_Age.Text)));
                    MessageBox.Show("Запись отредактированно", "OK", MessageBoxButton.OK);
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка", MessageBoxButton.OK);
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if ((Application.Current as App).persona != null)
            {
                TB_ID.Text = (Application.Current as App).persona.ID.ToString();
                TB_FName.Text = (Application.Current as App).persona.Fname;
                TB_LName.Text  = (Application.Current as App).persona.Lname;
                TB_Age.Text = (Application.Current as App).persona.Age.ToString();
            }
        }

        private void Lbl_Back_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            this.NavigationService.GoBack();
            //this.NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
            e.Complete();
            e.Handled = true;
        }

    }
}